package net.zhuike.novel.app.ui.adapter;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.support.BookMark;
import net.zhuike.novel.app.manager.SettingManager;
import net.zhuike.novel.app.ui.activity.ReadMenuActivity;

import java.util.List;

import framework.common.easyadpter.abslistview.EasyLVAdapter;
import framework.common.easyadpter.abslistview.EasyLVHolder;

/**
 * @author wq.
 * @date 2017/6/19.
 */
public class BookMarkAdapter extends BaseAdapter {
    String bookId;
    public List<BookMark> list;
    private ReadMenuActivity context;
    private LayoutInflater flater;
    public BookMarkAdapter(ReadMenuActivity context, List<BookMark> list) {
        this.context = context;
        this.list = list;
        this.flater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = flater.inflate(R.layout.item_read_mark, null);
            holder.tvMarkItem = (TextView) convertView
                    .findViewById(R.id.tvMarkItem);
            holder.tvMarkTitle = (TextView) convertView
                    .findViewById(R.id.tvMarkTitle);
            holder.markRemove = (TextView) convertView
                    .findViewById(R.id.markRemove);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.markRemove.setTag(position);
        if (list != null) {
            BookMark item = list.get(position);
            if (item.title != null) {
                SpannableString spanText = new SpannableString((position + 1) + ". " + item.title + ": ");
                spanText.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.text_dark_color)),
                        0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

                holder.tvMarkTitle.setText(spanText);
            }
            if (item.desc != null) {
                holder.tvMarkItem.setText(item.desc
                        .replaceAll("　", "")
                        .replaceAll(" ", "")
                        .replaceAll("\n", ""));
            }

            holder.markRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int num=(Integer) v.getTag();
                    context.revomeMark(num);
                }
            });
        }
        return convertView;
    }

    static class ViewHolder {
        public TextView tvMarkTitle;
        public TextView tvMarkItem;
        public TextView markRemove;
    }


}
